package com.topcheer.base.gatewaynacos.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;

import java.util.function.Consumer;

/**
 * @author xutao
 * @Package com.topcheer.gateway.filter
 * @Description: ${todo}
 * @date 2020/1/4 17:27
 */
@Slf4j
public class BlackListGatewayFilterFactory extends AbstractGatewayFilterFactory<BlackListGatewayFilterFactory.Config> {

    public BlackListGatewayFilterFactory() {
        super(BlackListGatewayFilterFactory.Config.class);
    }

    @Override
    public GatewayFilter apply(String routeId, Consumer<Config> consumer) {
        BlackListGatewayFilterFactory.Config config = (BlackListGatewayFilterFactory.Config)this.newConfig();
        consumer.accept(config);
        return this.apply(config);
    }

    @Override
    public GatewayFilter apply(BlackListGatewayFilterFactory.Config config) {
        String[] paths = config.getPath().split(",");
        return new BlackListFilter(paths);
    }

    public static class Config {
        private String path;

        public String getPath() {
            return path;
        }

        public BlackListGatewayFilterFactory.Config setPath(String path) {
            this.path = path;
            return this;
        }
    }
}
